package com.zhiyou100.video.webaction.video;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.zhiyou100.video.model.Course;
import com.zhiyou100.video.model.ResultMessage;
import com.zhiyou100.video.model.Speaker;
import com.zhiyou100.video.model.Subject;
import com.zhiyou100.video.model.Video;
import com.zhiyou100.video.service.CourseService;
import com.zhiyou100.video.service.SpeakerService;
import com.zhiyou100.video.service.VideoService;
import com.zhiyou100.video.utils.Page;
@SuppressWarnings("all")
@Controller("VideoAction")
@Scope(scopeName="prototype")
public class VideoAction extends ActionSupport implements ModelDriven<Video>{
	private Integer speaker;
	private Integer course;
	private String vodeotitlel;
	private Integer page;
	private Integer ssid;
	private Integer ccid;
	
	private List<Integer> thecheckbox;

	public List<Integer> getThecheckbox() {
		return thecheckbox;
	}

	public void setThecheckbox(List<Integer> thecheckbox) {
		this.thecheckbox = thecheckbox;
	}

	
	private Video video = new Video();
	@Override
	public Video getModel() {
		return video;
	}
	
	

	public Integer getSsid() {
		return ssid;
	}

	public void setSsid(Integer ssid) {
		this.ssid = ssid;
	}

	public Integer getCcid() {
		return ccid;
	}

	public void setCcid(Integer ccid) {
		this.ccid = ccid;
	}

	public Integer getSpeaker() {
		return speaker;
	}
	public void setSpeaker(Integer speaker) {
		this.speaker = speaker;
	}
	public Integer getCourse() {
		return course;
	}
	public void setCourse(Integer course) {
		this.course = course;
	}
	public String getVodeotitlel() {
		return vodeotitlel;
	}
	public void setVodeotitlel(String vodeotitlel) {
		this.vodeotitlel = vodeotitlel;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	@Autowired
	VideoService vs;
	public String videoList() throws Exception {
		speaker = speaker == null ? 0 :speaker;
		course = course == null ? 0 :course;
		vodeotitlel = vodeotitlel == null ? "" :vodeotitlel;
		Integer currentpage= page == null ? 1 :page;
	DetachedCriteria video = DetachedCriteria.forClass(Video.class);
	video.add(Restrictions.like("videoTitle",vodeotitlel, MatchMode.ANYWHERE));
	if(speaker!=0){
		video.add(Restrictions.eq("sid.id", speaker));
	}
	if(course!=0){
		video.add(Restrictions.eq("cid.id", course));
	}
	List<Speaker> slist=vs.selectSpeaker();
	List<Course>  clist = vs.selectCourse();
	Page<Video> listvideo = vs.selectVideo(video,currentpage);
	ActionContext.getContext().put("slist", slist);
	ActionContext.getContext().put("clist", clist);
	ActionContext.getContext().put("pagep", listvideo);
	return SUCCESS;
	}
	public String addVideo() throws Exception {
		List<Speaker> slist=vs.selectSpeaker();
		List<Course>  clist = vs.selectCourse();
		ActionContext.getContext().put("slist", slist);
		ActionContext.getContext().put("clist", clist);
	return "videoAdd";
	}
	public String videoAdd() throws Exception {
		Speaker speaker = new Speaker();
		speaker.setId(ssid);
		Course course = new Course();
		course.setId(ccid);
		video.setCid(course);
		video.setSid(speaker);
		System.out.println(video);
		vs.addVideo(video);
		return "videolist";
	}
	public String managevideo() throws Exception {
		Video vi = vs.selectVideol(video.getId());
		List<Speaker> slist=vs.selectSpeaker();
		List<Course>  clist = vs.selectCourse();
		
		ActionContext.getContext().put("slist", slist);
		ActionContext.getContext().put("clist", clist);
		ActionContext.getContext().put("video", vi);
		return "videomanage";
	}
	public String deleteAll() throws Exception {
	
		for (Integer cou : thecheckbox) {
			vs.deleteVideo(cou);
		}
		return "videolist";
	}
	
}
